package com.itheima.tlias84.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.controller.po.LoginParam;
import com.itheima.tlias84.dao.entity.Emp;
import com.itheima.tlias84.dao.mapper.EmpMapper;
import com.itheima.tlias84.service.LoginService;
import com.itheima.tlias84.utils.JwtUtil;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    EmpMapper empMapper;

    @Override
    public ResultUtil login(LoginParam param) {
        Emp emp = empMapper.selectByUsername(param.getUsername());
        if (Objects.isNull(emp) || !emp.getPassword().equals(param.getPassword())) {
            return ResultUtil.failure("用户名或密码错误");
        }
        String token = JwtUtil.generateToken(emp.getUsername(), emp.getPassword(), 30);
        JSONObject rs = new JSONObject();
        rs.put("id", emp.getId());
        rs.put("username", emp.getUsername());
        rs.put("name", emp.getName());
        rs.put("token", token);
        return ResultUtil.success(rs);
    }
}
